package com.yanqu.road.dao.impl.activity.tongbing;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tongbing.join.CrossTongBingUnionJoin;
import com.yanqu.road.entity.activity.tongbing.join.CrossTongBingUnionJoinMember;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTongBingUnionJoinDaoImpl extends TempDataObjectDao<CrossTongBingUnionJoin> {

    public static String getCrossTongBingUnionJoinSql = "select * from `t_c_tongbing_union_join` where `activityId`=? and groupId=? ";

    public Map<String, CrossTongBingUnionJoin> getCrossTongBingUnionJoinMap(int activityId, int groupId) {
        Map<String, CrossTongBingUnionJoin> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossTongBingUnionJoinSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossTongBingUnionJoin parse = parse(rs);
                map.put(parse.getUnionUid(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossTongBingUnionJoinSql = "update `t_c_tongbing_union_join` set unionBaseInfo=?,memberMap=?,campId=?,totalAbility=?," +
            "compressedAbility=?,oldRank=?,compressRank=? where activityId=? and groupId=? and unionUid=?";

    @Override
    protected boolean updateData(CrossTongBingUnionJoin crossTongBingUnionJoin) {
        crossTongBingUnionJoin.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUnionJoin.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUnionJoin.getMemberMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUnionJoin.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUnionJoin.getTotalAbility()));
        para.put(i++, new DbParameter(Types.VARCHAR, String.valueOf(crossTongBingUnionJoin.getCompressedAbility())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUnionJoin.getOldRank()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUnionJoin.getCompressRank()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUnionJoin.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUnionJoin.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTongBingUnionJoin.getUnionUid()));
        if (getDb().executeNoneQuery(updateCrossTongBingUnionJoinSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossTongBingUnionJoinSql = "insert into `t_c_tongbing_union_join`(activityId,groupId,unionUid,unionBaseInfo," +
            "memberMap,campId,totalAbility,compressedAbility,oldRank,compressRank) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossTongBingUnionJoin crossTongBingUnionJoin) {
        crossTongBingUnionJoin.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUnionJoin.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUnionJoin.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossTongBingUnionJoin.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUnionJoin.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossTongBingUnionJoin.getMemberMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUnionJoin.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossTongBingUnionJoin.getTotalAbility()));
        para.put(i++, new DbParameter(Types.VARCHAR, String.valueOf(crossTongBingUnionJoin.getCompressedAbility())));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUnionJoin.getOldRank()));
        para.put(i++, new DbParameter(Types.INTEGER, crossTongBingUnionJoin.getCompressRank()));
        if (getDb().executeNoneQuery(insertCrossTongBingUnionJoinSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossTongBingUnionJoin parseData(ResultSet rs) throws SQLException {
        CrossTongBingUnionJoin crossTongBingUnionJoin = new CrossTongBingUnionJoin();
        crossTongBingUnionJoin.setActivityId(rs.getInt("activityId"));
        crossTongBingUnionJoin.setGroupId(rs.getInt("groupId"));
        crossTongBingUnionJoin.setUnionUid(rs.getString("unionUid"));
        crossTongBingUnionJoin.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));
        crossTongBingUnionJoin.setMemberMap(JSON.parseObject(rs.getString("memberMap"), new TypeReference<ConcurrentHashMap<Long, CrossTongBingUnionJoinMember>>() {
        }));
        crossTongBingUnionJoin.setCampId(rs.getInt("campId"));
        crossTongBingUnionJoin.setTotalAbility(rs.getLong("totalAbility"));
        crossTongBingUnionJoin.setCompressedAbility(rs.getBigDecimal("compressedAbility"));
        crossTongBingUnionJoin.setOldRank(rs.getInt("oldRank"));
        crossTongBingUnionJoin.setCompressRank(rs.getInt("compressRank"));
        return crossTongBingUnionJoin;
    }

}
